package com.miracle.system.service.impl;

import com.miracle.common.utils.SecurityUtils;
import com.miracle.common.utils.StringUtils;
import com.miracle.system.domain.SysRole;
import com.miracle.system.domain.SysRoleUser;
import com.miracle.system.mapper.SysRoleMapper;
import com.miracle.system.mapper.SysRoleUserMapper;
import org.apache.commons.collections4.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.miracle.system.domain.SysUser;
import com.miracle.system.mapper.SysUserMapper;
import com.miracle.system.service.SysUserService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户 业务层处理
 *
 * @author miracle
 */
@Service
public class SysUserServiceImpl implements SysUserService
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Resource
    private SysUserMapper userMapper;
    @Resource
    private SysRoleUserMapper roleUserMapper;
    @Resource
    private SysRoleMapper roleMapper;

    /**
     * 通过用户名查询用户
     *
     * @param userName 用户名
     * @return 用户对象信息
     */
    @Override
    public SysUser selectUserByUserName(String userName)
    {
        return userMapper.selectUserByUserName(userName);
    }

    @Override
    public List<SysUser> selectUserList(SysUser user) {
        return userMapper.selectUserList(user);
    }

    @Override
    public SysUser selectUserByUserId(Integer userId) {
        SysUser user = userMapper.selectUserByUserId(userId);
        List<SysRole> roleList = roleMapper.selectRoleByUserId(userId);
        user.setUserRoles(roleList);
        return user;
    }

    @Override
    @Transactional
    public Integer insertUser(SysUser user) {
        Integer rows = userMapper.insertUser(user);
        if(rows > 0 && user.getUserRoles().size()>0){
            insertRoleUser(user);
        }
        return rows;
    }

    @Override
    @Transactional
    public Integer deleteUserByUserIds(Integer[] userIds) {
        deleteRoleUserByUserIds(userIds);
        return userMapper.deleteUserByUserIds(userIds);
    }

    @Override
    @Transactional
    public Integer updateUser(SysUser user) {

        updateRoleUser(user);
        return userMapper.updateUser(user);
    }

    @Override
    public Integer resetPassword(Integer[] userIds) {

        Map<String, Object> map = new HashedMap<String, Object>();
        String defaultPwd = SecurityUtils.encryptPassword("123456");

        map.put("password", defaultPwd);
        map.put("salt", SecurityUtils.getSalt(defaultPwd));
        map.put("lastUpdateTime", SecurityUtils.getUserId());
        map.put("userIds", userIds);

        return userMapper.resetPassword(map);
    }

    @Override
    public SysUser remoteCheckUser(SysUser user) {
        return userMapper.remoteCheckUser(user);
    }

    private void insertRoleUser(SysUser user){
        List<SysRole> roleList = user.getUserRoles();
        if (roleList.size() > 0){
            List<SysRoleUser> roleUserList = new ArrayList<SysRoleUser>();
            for (SysRole role: roleList) {
                SysRoleUser roleUser = new SysRoleUser();
                roleUser.setUserId(user.getUserId());
                roleUser.setRoleId(role.getRoleId());
                roleUser.setCreateBy(user.getCreateBy());
                roleUserList.add(roleUser);
            }
            roleUserMapper.batchInsertRoleUser(roleUserList);
        }
    }

    private void deleteRoleUserByUserIds(Integer[] userIds){
        roleUserMapper.deleteRoleUserByUserIds(userIds);
    }

    private void updateRoleUser(SysUser user) {
        List<SysRole> newRoleList = user.getUserRoles();
        if (newRoleList.size() > 0){
            roleUserMapper.deleteRoleUserByUserIdAndNoRole(user.getUserId(), newRoleList);
            insertRoleUser(user);
        }
    }
}
